package com.wechat.admin.service.impl;

import com.wechat.admin.model.dto.LoginQrCodeDto;
import com.wechat.admin.model.dto.UserInfoDto;
import com.wechat.admin.model.dto.WeChatLoginInfoDto;
import com.wechat.admin.model.dto.WeDeviceDto;
import com.wechat.admin.model.entity.User;
import com.wechat.admin.model.vo.LoginQrCodeVo;
import com.wechat.admin.model.vo.UserAndDeviceVo;
import com.wechat.admin.model.vo.WeChatLoginInfoVo;
import com.wechat.admin.service.AssembleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author:sjb
 * @CreateTime:2024-12-16
 * @Description: 组装数据服务
 * @Version：1.0
 */
@Service
public class AssembleServiceImpl implements AssembleService {

    /**
     * 转给前段生成二维码的Vo
     * @param loginQrCodeDto
     * @return
     */
    @Override
    public LoginQrCodeVo dtoToQrCodeVo(String weChatToken,LoginQrCodeDto loginQrCodeDto){
        LoginQrCodeVo loginQrCodeVo = new LoginQrCodeVo();
        loginQrCodeVo.setAppId(loginQrCodeDto.getAppId());
        loginQrCodeVo.setUuid(loginQrCodeDto.getUuid());
        loginQrCodeVo.setQrData(loginQrCodeDto.getQrData());
        loginQrCodeVo.setQrImgBase64(loginQrCodeDto.getQrImgBase64());
        loginQrCodeVo.setToken(weChatToken);
        return loginQrCodeVo;
    }

    @Override
    public WeChatLoginInfoVo dtoToWeChatLoginInfoVo(String appId,WeChatLoginInfoDto weChatLoginInfoDto) {
        WeChatLoginInfoVo weChatLoginInfoVo = new WeChatLoginInfoVo();
        weChatLoginInfoVo.setWeChatLoginStatus("登录成功");
        weChatLoginInfoVo.setWxId(weChatLoginInfoDto.getWxid());
        weChatLoginInfoVo.setNickName(weChatLoginInfoDto.getNickName());
        weChatLoginInfoVo.setAlias(weChatLoginInfoDto.getAlias());
        weChatLoginInfoVo.setDeviceId(appId);
        return weChatLoginInfoVo;
    }

    @Override
    public List<UserAndDeviceVo.UserDeviceInfo> assembleInfoAndData(List<UserInfoDto> userList, Map<Long,
            List<WeDeviceDto>> deviceListByUserIds) {

        List<UserAndDeviceVo.UserDeviceInfo> records = userList.stream().map(user -> {
            UserAndDeviceVo.UserDeviceInfo userDeviceInfo = new UserAndDeviceVo.UserDeviceInfo();
            userDeviceInfo.setUserId(user.getId());
            userDeviceInfo.setUsername(user.getUserAccount());
            userDeviceInfo.setRole(user.getUserRole());
            userDeviceInfo.setUserGroup(user.getUserGroup());
            userDeviceInfo.setGroupName(user.getGroupName());

            List<UserAndDeviceVo.DeviceInfo> deviceInfos = deviceListByUserIds.getOrDefault(user.getId(), new ArrayList<>())
                    .stream()
                    .map(device -> {
                        UserAndDeviceVo.DeviceInfo deviceInfo = new UserAndDeviceVo.DeviceInfo();
                        deviceInfo.setDeviceId(device.getDeviceId());
                        deviceInfo.setToken(device.getToken());
                        deviceInfo.setDeviceName(device.getDeviceName());
                        deviceInfo.setStatus(device.getStatus());
                        deviceInfo.setLoginTime(device.getLoginTime());
                        deviceInfo.setJoinedAt(device.getAssignedAtTime());
                        deviceInfo.setDescription(device.getDescription());
                        return deviceInfo;
                    }).collect(Collectors.toList());
            userDeviceInfo.setDevices(deviceInfos);
            return userDeviceInfo;
        }).collect(Collectors.toList());

        return records;
    }
}
